﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BusinessFacade;
using Comm;
using Entities;

namespace MedicareNoteManagerWeb.Web
{
    public partial class Function : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            if (!Page.IsPostBack)
            {
                BindDrop();
                Refurbish();
                //PageGrid1_SelectedValueChanged(sender, e);
            }
        }
        public void BindDrop()
        {
            FillDropDownList("GetMenuDropList", "MenuID", "MenuName", "全部模块", this.DropFunction);
        }

        //protected void PageGrid1_Invalidate(object sender, EventArgs e)
        //{
        //    DataTable dt = Busi_sys_Menu.Getlist(int.Parse(DropFunction.SelectedValue));
        //    gv_ShowFuction.DataSource = dt;
        //    gv_ShowFuction.DataBind();
        //}

        public void Refurbish()
        {
            DataTable dt = Busi_sys_Menu.Getlist(int.Parse(DropFunction.SelectedValue));
            gv_ShowFuction.DataSource = dt;
            gv_ShowFuction.DataBind();
        }

        protected void BtnFind_Click(object sender, EventArgs e)
        {
            int select = int.Parse(this.DropFunction.SelectedValue.ToString());
            Refurbish();
            //PageGrid1_SelectedValueChanged(sender, e);
        }

        public void GetModel(sys_menu_Model model)
        {
            //model.MenuID = gv_ShowFuction.SelectedValue == null ? 0 : int.Parse(gv_ShowFuction.SelectedValue.ToString());
            //model.MenuName = TextBox1.Text.Trim();
            //model.MenuParentNo = int.Parse(DropFunction0.SelectedValue);
            //model.MenuUrl = TextBox2.Text.Trim();
        }

        sys_menu_Model model = new sys_menu_Model();
        Busi_sys_Menu bss = new Busi_sys_Menu();
        protected void gv_ShowFuction_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); //获取被点击的linkButton所在的GridViewRow
                model.Menuselectedid = gvrow.RowIndex; //获取到行索引
                Label lb_delete = gv_ShowFuction.Rows[model.Menuselectedid].FindControl("lbl_FunctionID") as Label;
                model.MenuID = int.Parse(lb_delete.Text);
            }
        }

        protected void gv_ShowFuction_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            bool result = Busi_sys_Menu.Delete(model.MenuID);
            if (result == true)
            {
                MessageBox.ShowDialog(this.Page, "删除成功！", "success");
                Refurbish();
            }
            else
            {
                MessageBox.ShowDialog(this.Page, "删除失败！", "error");
                Refurbish();
            }
        }

        protected static int number;
        protected void gv_ShowFuction_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv_ShowFuction.EditIndex = e.NewEditIndex;
            Label lbl_FunctionID = gv_ShowFuction.Rows[gv_ShowFuction.EditIndex].FindControl("lbl_FunctionID") as Label;
            number = int.Parse(lbl_FunctionID.Text);
            Refurbish();
        }
        protected void gv_ShowFuction_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv_ShowFuction.EditIndex = -1;
            Refurbish();
        }
        protected void gv_ShowFuction_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DropDownList ddlClass = new DropDownList();
            ddlClass = (DropDownList)e.Row.FindControl("ddl_Class");
            TextBox tbName = new TextBox();
            tbName = (TextBox)e.Row.FindControl("tb_FunctionName");
            TextBox tbURL = new TextBox();
            tbURL = (TextBox)e.Row.FindControl("tb_URL");
            if (ddlClass != null && tbName != null && tbURL!=null)
            {
                tbName.Text = bss.GetName(number);
                tbURL.Text = bss.GetUrl(number);
                FillDropDownList("GetMenuDropList", "MenuID", "MenuName", "全部模块", ddlClass);
                DataTable dt = bss.GetBing(number);
                ddlClass.SelectedValue = dt.Rows.Count > 0 ? dt.Rows[0]["MenuParentNO"].ToString() : "0";
            }
        }

        protected void gv_ShowFuction_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                string strErr="";
                DropDownList ddlClass = (DropDownList)gv_ShowFuction.Rows[e.RowIndex].FindControl("ddl_Class");
                TextBox tbName = (TextBox)gv_ShowFuction.Rows[e.RowIndex].FindControl("tb_FunctionName");
                TextBox tbURL = (TextBox)gv_ShowFuction.Rows[e.RowIndex].FindControl("tb_URL");
                model.MenuName = tbName.Text.ToString();
                model.MenuUrl = tbURL.Text.ToString();
                model.MenuID = number;
                model.MenuParentNo = int.Parse(ddlClass.SelectedValue.ToString());
                if (string.IsNullOrEmpty(tbName.Text.Trim()))
                {
                    strErr += "功能模块名称不能为空";
                }
                if (string.IsNullOrEmpty(tbURL.Text.Trim()))
                {
                    strErr += "功能模块Url不能为空";
                }
                if (ddlClass.SelectedValue == "0")
                {
                    strErr += "请选择功能模块";
                }
                if (!string.IsNullOrEmpty(strErr))
                {
                    MessageBox.ShowDialog(Page, strErr, "error");
                }
                else 
                {
                    Busi_sys_Menu.Update1(model);
                    MessageBox.ShowDialog(Page, "更新成功！", "success");
                    gv_ShowFuction.EditIndex = -1;
                    Refurbish();
                }
            }
            catch
            {
                MessageBox.ShowDialog(Page, "更新失败！", "error");

                gv_ShowFuction.EditIndex = -1;
                Refurbish();
            }
        }

        
        //protected void PageGrid1_SelectedValueChanged(object sender, EventArgs e)
        //{
        //    sys_menu_Model model = new Busi_sys_Menu().GetModel1(gv_ShowFuction.SelectedValue == null ? 0 : int.Parse(gv_ShowFuction.SelectedValue.ToString()));
        //    DropFunction0.SelectedValue = model.MenuParentNo.ToString();
        //    TextBox1.Text = model.MenuName;
        //    TextBox2.Text = model.MenuUrl;
        //}

        //protected void btnOK_Click(object sender, EventArgs e)
        //{
        //    sys_menu_Model model = new sys_menu_Model();
        //    GetModel(model);
        //    if (!string.IsNullOrEmpty(CheckData()))
        //    {
        //        MessageBox.ShowDialog(Page, CheckData(), "error");
        //        //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.error('" + CheckData() + "'); </script>");
        //    }
        //    else
        //    {
        //        if (gv_ShowFuction.SelectedValue == null)
        //        {
        //            if (Busi_sys_Menu.Add1(model))
        //            {

        //                //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.success('添加成功！'); </script>");
        //                MessageBox.ShowDialog(Page, "添加成功！", "success");
        //                PageGrid1_Invalidate(sender, e);
        //                PageGrid1_SelectedValueChanged(sender, e);
        //            }
        //            else
        //            {
        //                //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.error('添加失败！'); </script>");
        //                MessageBox.ShowDialog(Page, "添加失败！", "error");
        //                PageGrid1_Invalidate(sender, e);
        //                PageGrid1_SelectedValueChanged(sender, e);
        //            }
        //        }
        //        else
        //        {
        //            try
        //            {
        //                Busi_sys_Menu.Update1(model);
        //                MessageBox.ShowDialog(Page, "更新成功！", "success");
        //                //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.success('更新成功！'); </script>");
        //                PageGrid1_Invalidate(sender, e);
        //                PageGrid1_SelectedValueChanged(sender, e);
        //            }
        //            catch (System.Exception ex)
        //            {
        //                MessageBox.ShowDialog(Page, "更新失败！" + ex.Message, "error");
        //                //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.error('更新失败！" + ex.Message + "'); </script>");
        //                PageGrid1_Invalidate(sender, e);
        //                PageGrid1_SelectedValueChanged(sender, e);
        //            }
        //        }
        //    }
        //}
        //protected void BtnDel_Click(object sender, EventArgs e)
        //{
        //    if (Busi_sys_Menu.Delete(int.Parse(gv_ShowFuction.SelectedValue.ToString())))
        //    {
        //        MessageBox.ShowDialog(Page, "删除成功！", "success");
        //        //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.success('删除成功！'); </script>");
        //        PageGrid1_Invalidate(sender, e);
        //        //PageGrid1_SelectedValueChanged(sender, e);
        //    }
        //    else
        //    {
        //        //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.success('删除失败！'); </script>");
        //        MessageBox.ShowDialog(Page, "删除失败！", "error");
        //        PageGrid1_Invalidate(sender, e);
        //        //PageGrid1_SelectedValueChanged(sender, e);
        //    }
        //}
    }
}